package com.cy.store.service.impl;

import com.cy.store.mapper.CartVOMapper;
import com.cy.store.service.ICartVOService;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/3/1 15:41
 */
@Service
public class CartVOServiceImpl implements ICartVOService {
    @Autowired
    private CartVOMapper cartVOMapper;

    @Override
    public List<CartVO> findByUid(Integer uid) {
        return cartVOMapper.findVOByUid(uid);
    }

    @Override
    public List<CartVO> getOVByCids(Integer uid, Integer[] cids) {
// 根据商品ID列表查询购物车信息
        List<CartVO> list = cartVOMapper.findVOByCids(cids);
        Iterator<CartVO> iterator = list.iterator();
// 遍历查询结果，移除不属于当前用户的购物车项
        while (iterator.hasNext()) {
            CartVO cartVO = iterator.next();
            // 当购物车项的用户ID与当前用户UID不匹配时，从列表中移除
            if (!cartVO.getUid().equals(uid)) iterator.remove();
        }
// 返回处理后的购物车项列表
        return list;

    }
}
